package cn.edu.sig.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import cn.edu.sig.bean.CurrentPage;
import cn.edu.sig.bean.Goods;
import cn.edu.sig.bean.Style;
import cn.edu.sig.bean.User;
import cn.edu.sig.dao.GoodsDao;
import cn.edu.sig.util.PaginationHelper;

@Repository("goodsDao")
public class GoodsDaoImpl extends TemplateSupport implements GoodsDao {

	public void add(Goods goods) {

		String sql = this.sqlSource.getSql("goods.add");

		Object[] param = new Object[] { goods.getGoodsName(), goods.getPrice(),
				goods.getAccount(), goods.getFineness(), goods.getContent(),
				goods.getCreateTime(), goods.getChangeType(),
				goods.getUser().getCode(), goods.getStyle().getStyleId() };

		this.getJdbcTemplate().update(sql, param);

	}

	public void delete(Integer goodsId) {

		String sql = this.sqlSource.getSql("goods.delete");

		this.getJdbcTemplate().update(sql, goodsId);

	}

	public void edit(Goods goods) {
		String sql = this.sqlSource.getSql("goods.edit");

		Object[] param = new Object[] { goods.getGoodsName(), goods.getPrice(),
				goods.getAccount(), goods.getFineness(), goods.getContent(),
				goods.getCreateTime(), goods.getChangeType(),
				goods.getUser().getCode(), goods.getStyle().getStyleId() };

		this.getJdbcTemplate().update(sql, param);

	}

	public List<Goods> findAll() {
		String sql = this.sqlSource.getSql("goods.list");

		RowMapper<Goods> mapper = new RowMapper<Goods>() {
			public Goods mapRow(ResultSet rs, int rowNum) throws SQLException {

				User user = new User();
				user.setCode(rs.getString("code"));
				Style style = new Style();
				style.setStyleId(rs.getInt("styleId"));
				// Message message = new Message();
				Goods goods = new Goods();
				goods.setGoodsId(rs.getInt("goodsId"));
				goods.setGoodsName(rs.getString("goodsName"));
				goods.setUser(user);
				goods.setStyle(style);
				goods.setPrice(rs.getFloat("price"));
				goods.setAccount(rs.getInt("account"));
				goods.setFineness(rs.getString("fineness"));
				goods.setContent(rs.getString("content"));
				goods.setChangeType(rs.getString("changeType"));

				return goods;
			}
		};

		return this.getJdbcTemplate().query(sql, mapper);
	}

	public Goods findById(String goodsId) {
		String sql = this.sqlSource.getSql("goods.goods");

		final Goods goods = new Goods();

		this.getJdbcTemplate().query(sql, new Object[] { goodsId },
				new RowCallbackHandler() {

					public void processRow(ResultSet rs) throws SQLException {
						User user = new User();
						user.setCode(rs.getString("code"));
						Style style = new Style();
						goods.setUser(user);
						goods.setStyle(style);
						style.setStyleId(rs.getInt("styleId"));
						goods.setGoodsId(rs.getInt("goodsId"));
						goods.setGoodsName(rs.getString("goodsName"));
						goods.setPrice(rs.getFloat("price"));
						goods.setAccount(rs.getInt("account"));
						goods.setFineness(rs.getString("fineness"));
						goods.setContent(rs.getString("content"));
						goods.setChangeType(rs.getString("changeType"));

					}
				});

		return goods;
	}

	public Goods findByName(String name) {

		return null;
	}

	public List<Goods> findAllByGoods(Goods goods) {
		String sql = this.sqlSource.getSql("user.goods.list");

		RowMapper<Goods> mapper = new RowMapper<Goods>() {
			public Goods mapRow(ResultSet rs, int rowNum) throws SQLException {

				User user = new User();
				user.setCode(rs.getString("code"));
				Style style = new Style();
				style.setStyleId(rs.getInt("styleId"));
				// Message message = new Message();
				Goods goods = new Goods();

				goods.setGoodsId(rs.getInt("goodsId"));
				goods.setGoodsName(rs.getString("goodsName"));
				goods.setUser(user);
				goods.setStyle(style);
				goods.setPrice(rs.getFloat("price"));
				goods.setAccount(rs.getInt("account"));
				goods.setFineness(rs.getString("fineness"));
				goods.setContent(rs.getString("content"));
				goods.setChangeType(rs.getString("changeType"));

				return goods;
			}
		};

		return this.getJdbcTemplate().query(sql,
				new Object[] { goods.getUser().getCode() }, mapper);
	}

	public CurrentPage<Goods> findAllByUser(Goods goods, int pageNo,
			int pageSize) {

		String sqlCountRows = "select count(*) from goods where code = ?";
		String sqlFetchRows = "select * from goods where code = ? limit ? , ?";

		return findAll(goods, sqlCountRows, sqlFetchRows, pageNo, pageSize);

	}

	public CurrentPage<Goods> findAllByUser(int pageNo, int pageSize) {

		String sqlCountRows = "select count(*) from goods ";
		String sqlFetchRows = "select * from goods limit ? , ?";
		Goods goods = new Goods();
		User user = new User();
		user.setCode("");
		goods.setUser(user);
		return findAll(goods, sqlCountRows, sqlFetchRows, pageNo, pageSize);
	}

	public CurrentPage<Goods> findAll(Goods goods, String sqlCountRows,
			String sqlFetchRows, int pageNo, int pageSize) {

		PaginationHelper<Goods> ph = new PaginationHelper<Goods>();

		return ph.fetchPage(this.getJdbcTemplate(), sqlCountRows, sqlFetchRows,
				goods.getUser().getCode(), pageNo, pageSize,
				new ParameterizedRowMapper<Goods>() {
					public Goods mapRow(ResultSet rs, int i)
							throws SQLException {

						User user = new User();
						user.setCode(rs.getString("code"));
						Style style = new Style();
						style.setStyleId(rs.getInt("styleId"));
						Goods goods = new Goods();

						goods.setGoodsId(rs.getInt("goodsId"));
						goods.setGoodsName(rs.getString("goodsName"));
						goods.setUser(user);
						goods.setStyle(style);
						goods.setPrice(rs.getFloat("price"));
						goods.setAccount(rs.getInt("account"));
						goods.setFineness(rs.getString("fineness"));
						goods.setContent(rs.getString("content"));
						goods.setChangeType(rs.getString("changeType"));

						return goods;
					}
				});

	}

}
